package cn.yunyichina.provider.hquery.thread;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.framework.data.SpringContextHandler;
import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.framework.exception.CallException;
import cn.yunyichina.provider.hquery.dao.ReportImageDao;
import cn.yunyichina.provider.hquery.entity.ReportImage;
import cn.yunyichina.provider.hquery.vo.PhysicalExamVo;
import cn.yunyichina.provider.iface.entity.base.MedicalCardVo;
import cn.yunyichina.provider.iface.entity.base.Org;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.iface.entity.hisiface.physical.PhysicalReportImageVoResponse;
import cn.yunyichina.utils.convert.JsonUtils;

/**
 * Created by 15-AK on 2016/12/15.
 */
public class PhysicalReportListRunnable  implements Runnable{
	
    private static final Logger logger = LoggerFactory.getLogger(PhysicalReportListRunnable.class);
    
    private List<PhysicalReportImageVoResponse> reportImageVoResponseList;
    
    private PhysicalExamVo requestVo;
    
    private ReportImageDao reportImageDao = SpringContextHandler.getBean(ReportImageDao.class);
    
    public PhysicalReportListRunnable(List<PhysicalReportImageVoResponse> reportImageVoResponse,PhysicalExamVo requestVo) {
        this.reportImageVoResponseList = reportImageVoResponse;
        this.requestVo = requestVo;
    }

    @Override
    public void run() {

        logger.info("保存检验检查图片记录开始======reportImageVoResponseList ="+JsonUtils.toJsonString(""));
        
        //查询绑定卡
        logger.info("【检验检查图片记录保存线程处理-查询绑定卡信息】, yyt.base.medicalcard.by.cardno.get request=" + JSON.toJSONString(requestVo));
        MedicalCardVo medicalCard =  BaseService.getMedicalCardByCardNo(requestVo.getHospitalCode(),requestVo.getPatCardNo(),requestVo.getPatCardType(),requestVo.getAppId(),null);
        if (medicalCard == null) {
            throw new CallException("18010","找不到绑定卡信息 card_no=" +requestVo.getPatCardNo());
        }
        logger.info("【检验检查图片记录保存线程处理-查询绑定卡信息】, yyt.base.medicalcard.by.cardno.get response=" + JSONObject.toJSONString(medicalCard));
        // 查询组织信息-----------------------------------------------start
        Map<String, String> orgParamMap = new HashMap<String, String>();
        orgParamMap.put("org_code", requestVo.getHospitalCode());
        logger.info("【检验检查图片记录-查询组织信息】, yyt.base.branchhospital.org.list request=" + JSON.toJSONString(orgParamMap));
        Response orgResponse = BaseService.callUnderlineFromInternal("yyt.base.branchhospital.org.list", JSON.toJSONString(orgParamMap));
        logger.info("【检验检查图片记录-查询组织信息】, yyt.base.branchhospital.org.list response=" + JSONObject.toJSONString(orgResponse));
        if (!"0".equals(orgResponse.getResultCode())) {
            throw new CallException("18010","找不到机构信息 by org_code=" + requestVo.getHospitalCode());
        }
        List<Org> orgList = JsonUtils.parseArray(orgResponse.getResult(), Org.class);
        if (org.springframework.util.CollectionUtils.isEmpty(orgList)) {
            throw new CallException("18010","找不到机构信息 by org_code=" + requestVo.getHospitalCode());
        }
        Org org = orgList.get(0);
        //存检查基本信息
        if (reportImageVoResponseList != null && reportImageVoResponseList.size() > 0) {
			
			List<ReportImage> reportImageList = reportImageDao.selectByOrgCodeAndReportId(requestVo.getHospitalCode(), requestVo.getPhy_exam_id());
			
			//存检查基本信息
			ReportImage reportImage = null;
			if(reportImageList.size() == 0){
				for (PhysicalReportImageVoResponse vo : reportImageVoResponseList) {
					reportImage = new ReportImage();
					reportImage.setId(PKGenerator.generateId());
					reportImage.setOrgId(org.getId());
					reportImage.setOrgName(org.getOrgName());
					reportImage.setOrgCode(requestVo.getHospitalCode());
					reportImage.setBranchId("");
					reportImage.setBranchCode("");
					reportImage.setBranchName("");
					reportImage.setPatId(medicalCard.getPatId());
					reportImage.setPatName(medicalCard.getPatName());
					reportImage.setIdCardType(medicalCard.getIdCardType());
					reportImage.setIdCardNo(medicalCard.getIdCardNo());
					reportImage.setAdmissionNo(medicalCard.getAdmissionNo());
					reportImage.setReportId(requestVo.getPhy_exam_id());
					//体检报告类型为 3
					reportImage.setReportType((short) 3);
					reportImage.setFileAddress(vo.getFileAddress());
					reportImage.setFileContent(vo.getFileContent());
					reportImage.setFileType(vo.getFileType());
					reportImage.setCreatedTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
					//插入检验检查报告图片记录到数据表
	        		logger.info("插入检查报告图片记录=="+JsonUtils.toJsonString(""));
	        		reportImageDao.insert(reportImage);
				}
			}
        }
    }
}
